package com.seanliao.nav.controller;

import com.seanliao.nav.common.response.Result;
import com.seanliao.nav.common.response.ResultBuilder;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.session.SessionInformation;
import org.springframework.security.core.session.SessionRegistry;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 测试controller
 */
@RestController
@RequestMapping("/test")
@PreAuthorize("@pms.hasRoles('admin')")
@RequiredArgsConstructor
public class TestController {

    private final SessionRegistry sessionRegistry;

    @RequestMapping("/test")
    public Object test() {
        return "success";
    }

    @GetMapping("/list-principal")
    public Result<?> listPrincipal() {
        List<Object> list = sessionRegistry.getAllPrincipals();
        return ResultBuilder.success(list);
    }

    @GetMapping("/list-session/{index}")
    public Result<?> listSession(@PathVariable("index") Integer index) {
        List<Object> list = sessionRegistry.getAllPrincipals();
        List<SessionInformation> list2 = sessionRegistry.getAllSessions(list.get(index), true);
        return ResultBuilder.success(list2);
    }


}
